﻿<% @ LANGUAGE="VBSCRIPT" CODEPAGE="65001" %>
<% Option Explicit %>

<!-- #include file="../adovbs.inc" -->
<!-- #include file="checkfile.asp" -->

<%
    Dim id, ip
    Dim path, filename
    Dim checkMsg, logMsg

    On Error resume Next

    id = Request.QueryString("code")
    ip = Request.ServerVariables("REMOTE_ADDR")

    checkMsg = checkfile(id)

    If checkMsg Then
        path = Server.MapPath("/") & "\..\database\images\"
        filename = path & Replace(id,".","") & ".pc"
        Response.Expires = 0
        Response.Buffer = TRUE
        Response.Clear
        Response.AddHeader "Content-Disposition","attachment; filename=postcard.jpg"
        Response.ContentType = "image/jpeg"
        DecryptImage(filename)
        Response.Flush
        If Err.number <> 0 Then
            logMsg = insertLog(ip, id, "FAILED:" & Err.number & "-" & Err.Description )
            If logMsg <> "SUCCESS" Then
                Response.Write(logMsg)
            End If
        Else
            logMsg = insertLog(ip, id, "SUCCESS")
            If logMsg <> "SUCCESS" Then
                Response.Write(logMsg)
            End If
        End If
    Else
        Response.Write("FILE_NOT_FOUND")
        logMsg = insertLog(id, "FILE_NOT_FOUND")
        If logMsg <> "SUCCESS" Then
            Response.Write(logMsg)
        End If
    End If
   
    Function DecryptImage(filename)
      Dim i, size
      Dim BinaryStream

      Const adTypeBinary = 1
  
      'Create Stream object
      Set BinaryStream = CreateObject("ADODB.Stream")
  
      'Specify stream type - we want To get binary data.
      BinaryStream.Type = adTypeBinary
  
      'Open the stream
      BinaryStream.Open
  
      'Load the file data from disk To stream object
      BinaryStream.LoadFromFile fileName

      size = BinaryStream.Size
      BinaryStream.Position = size-20
      Response.BinaryWrite BinaryStream.Read(20)

      BinaryStream.Position = 20

      For i=21 To size-20   
        Response.BinaryWrite BinaryStream.Read(1)
      Next

      BinaryStream.Position = 0
      Response.BinaryWrite BinaryStream.Read(20)

      BinaryStream.Close
  
      Set BinaryStream = Nothing

    End Function

    Function insertLog(ip, id, status)
        Dim conn, cmd
        Dim dateTime

        On Error resume Next

        dateTime = FormatDateTime(Now(),0)

        Set conn = Server.CreateObject("ADODB.Connection")
        conn.Provider="Microsoft.Jet.OLEDB.4.0"
        conn.Open(Server.MapPath("/")+"\..\database\postcard.mdb")

        Set cmd = Server.CreateObject("ADODB.Command")
        cmd.ActiveConnection = conn
        cmd.CommandText = "INSERT INTO tblDownloadLog(IP,DownloadCode,Status,DownloadDateTime) VALUES(?,?,?,?)"
        cmd.CommandType = adCmdText
        cmd.Parameters.Append cmd.CreateParameter("@ip",adVarChar,adParamInput,50,0)
        cmd.Parameters.Append cmd.CreateParameter("@code",adVarChar,adParamInput,15,0)
        cmd.Parameters.Append cmd.CreateParameter("@status",adVarChar,adParamInput,100,0)
        cmd.Parameters.Append cmd.CreateParameter("@datetime",adVarChar,adParamInput,50,0)
        cmd.Parameters("@ip") = ip
        cmd.Parameters("@code") = id
        cmd.Parameters("@status") = status
        cmd.Parameters("@datetime") = dateTime
        cmd.Execute

        conn.Close

        If Err.number <> 0 Then
            insertLog = "DB_ERROR:" & Err.Source & "(" & Err.number & " - " & Err.Description & ")"
        ELSE
            insertLog = "SUCCESS"
        End If
    End Function
%>
